﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace XMLAnalyser.Source_Code
{
    class XMLAnalyser :IAnalyser
    {
        private string _fileDir = string.Empty;
        private string _fileName = string.Empty;
        protected string xmlFile;
        protected XmlDocument xmlDoc = null;

        public string FileName 
        {
            get { return _fileName; }
            set {
                if (value == null || string.Empty.Equals(value))
                    throw new MyException("File name should not be empty.");
                else 
                    _fileName = value;
            }
        }

        public string FileDir
        {
            get { return _fileDir; }
            set
            {
                if (string.Empty.Equals(value))
                    _fileDir = Directory.GetCurrentDirectory();
                else
                {
                    if (value.EndsWith("\\", StringComparison.CurrentCultureIgnoreCase))
                        _fileDir = value;
                    else
                        _fileDir = value + "\\";
                }
            }
        }

        private XMLAnalyser() : this(Directory.GetCurrentDirectory(), string.Empty) { }
        
        public XMLAnalyser(string fileName) : this(Directory.GetCurrentDirectory(), fileName){ }

        public XMLAnalyser(string fileDir, string fileName)
        {
            FileDir = fileDir;
            FileName = fileName;
            xmlFile = FileDir + FileName;
           
            // Create instance of XmlDocument
            xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
        }

        public string GetElementValue(string xPath)
        {
            XmlNode _node = xmlDoc.SelectSingleNode(xPath);
            if (_node == null)
                throw new MyException(string.Format("Cannot find element by: {0}", xPath));

            return _node.Value;
        }

        public Dictionary<XmlNode, XmlNode> GetElements(string xPath)
        {
            Dictionary<XmlNode, XmlNode> _dict = new Dictionary<XmlNode, XmlNode>();
            XmlNodeList _nodeList = xmlDoc.SelectNodes(xPath);           
            foreach(XmlNode _node in _nodeList)
            {
                foreach (XmlNode _temp in _node.ParentNode.ParentNode.ChildNodes)
                    if (string.Compare(_temp.Name, "Result", true) == 0)
                    {                       
                        _dict.Add(_temp, _node);
                        // break;
                    }
            }

            return _dict;
        }

        /*public IList<XmlNode> GetElements(string xPath) 
        {
            IList<XmlNode> nodeList = new List<XmlNode>;
            // XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xPath);
            // xmlNodeList.Get
            return nodeList;

        }*/

        public bool UpdateElement(string xPath, string value)
        {
            XmlNode _node = xmlDoc.SelectSingleNode(xPath);
            if(_node == null)
                throw new MyException(string.Format("Cannot find element by: {0}", xPath));

            _node.Value = value;
            xmlDoc.Save(xmlFile);

            return true;
        }

        public bool DeleteElement(string xPath)
        {
            XmlNode _node = xmlDoc.SelectSingleNode(xPath);
            if (_node == null)
                throw new MyException(string.Format("Cannot find element by: {0}", xPath));

            xmlDoc.RemoveChild(_node);

            return true;
        }       
    }
}
